# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.6-slim-stretch
SHELL ["/bin/sh","-c"]
LABEL vendor = Tony.Don\ Pro \ 
      com.movieapi.version="0.0.1-beta" \
      com.movieapi.release-date="2022-01-10" \
      com.movieapi.version.is-production="true" \
      com.movieapi.version.is-beta="true"

ENV GROUP=uwsgi USER=u_uwsgi PORT=6688
# database set
ENV DB_ENGINE="django.db.backends.mysql" DB_HOST="192.168.50.2" DB_PORT="3306" DB_NAME="spider" DB_USR="root" DB_PW="qfhwrepwq"
# web config
ENV SECRET="k48g\$c0%\$\$vw&!4rbrh+w3y\$o-@\$u*^jtayx1+-5q0cg=i1#=j" SPIDER_SERVER="http://127.0.0.1:9901" 
ENV DOMIAN="http://127.0.0.1:${PORT}"
# api config
ENV PAGE_SIZE='20' IS_DEBUG='False'
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
EXPOSE ${PORT}
# add shell group and user.
RUN groupadd -r -g 1000 ${GROUP} && useradd -r -g ${GROUP} ${USER} -u 1000
WORKDIR /app
ADD . /app
ADD requirements.txt .

# shell
# init config
RUN set -ex; \
    sed -i 's/{DB_ENGINE}/"'${DB_ENGINE}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{DB_HOST}/"'${DB_HOST}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{DB_PORT}/"'${DB_PORT}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{DB_NAME}/"'${DB_NAME}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{DB_USR}/"'${DB_USR}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{DB_PW}/"'${DB_PW}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{SECRET}/"'${SECRET}'"/g' /app/deploy/config.tmp.py \
    && sed -i 's/{PAGE_SIZE}/'${PAGE_SIZE}'/g' /app/deploy/config.tmp.py \
    && sed -i 's/{IS_DEBUG}/'${IS_DEBUG}'/g' /app/deploy/config.tmp.py \
    && sed -i 's!{SPIDER_SERVER}!"'${SPIDER_SERVER}'"!g' /app/deploy/config.tmp.py \
    && sed -i 's!{DOMIAN}!"'${DOMIAN}'"!g' /app/deploy/config.tmp.py \
    && sed -i 's/{PORT}/'${PORT}'/g' /app/deploy/config.tmp.py; \
    cp -f /app/deploy/config.tmp.py /app/src/config.py

# apt install args:
#     --assume-yes: auto install.
#     -no-install-recommends: not install package for non-essentials.
RUN applications="gcc wget"; \
    set -ex; \
    sed -i 's/{http_port}/'${PORT}'/g' /app/deploy/tmp_uwsgi.ini \
    && cp -f /app/deploy/tmp_uwsgi.ini /app/src/uwsgi.ini \
    && chmod 777 /app/src/uwsgi.ini \
    && chmod 777 /app/uwsgi; \
    mv /app/deploy/sources.list /etc/apt/sources.list \
    && apt -qq update \
    && apt -qq install --assume-yes $applications > /dev/null; \
    wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64" \
    && chmod +x /usr/local/bin/gosu \
    && gosu nobody true; \
    python -m pip install --upgrade --quiet pip \
    && pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install -I --no-cache-dir --quiet uwsgi \
    && ln -s /usr/local/bin/uwsgi /usr/bin/uwsgi; \
    python -m pip install -r requirements.txt; \
    python /app/manage.py collectstatic
#USER ${USER}
ENTRYPOINT ["/app/deploy/entrypoint.sh"]
CMD ["movieapi"]
